// Created by WXX on 2021/10/22 21:22
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {

        int n = grid.size(), m = grid[0].size();
        vector<vector<int>> f(n, vector<int>(m, INT_MAX));
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (!i && !j) f[i][j] = grid[i][j];
                else {
                    if (i) f[i][j] = min(f[i][j], f[i - 1][j] + grid[i][j]);
                    if (j) f[i][j] = min(f[i][j], f[i][j - 1] + grid[i][j]);
                }
        return f[n - 1][m - 1];
    }
};

int main() {

    vector<vector<int>> grid = {
            {1, 2},
            {1, 1}
    };
    cout << Solution().minPathSum(grid) << endl;  // 3

    return 0;
}
